/*
 * Copyright 2003-2005 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

using System;

using TypeElement = argot.TypeElement;
using TypeException = argot.TypeException;
using TypeInputStream = argot.TypeInputStream;
using TypeOutputStream = argot.TypeOutputStream;
using TypeReader = argot.TypeReader;
using TypeWriter = argot.TypeWriter;

namespace argot.common
{
	
	/// <summary>
	/// This is a byte array.  Basically for any binary data or storing
	/// other types of data as part of another mapped set.  It uses a
	/// single unsigned 16bit integer to specificy the size of the array.
	/// It returns and writes byte[].
	/// </summary>
	public class U16ArrayByte 
	:TypeReader, TypeWriter
	{
		
		public const System.String TYPENAME = "u16binary";
		
		public virtual System.Object Read(TypeInputStream inStream, TypeElement element)
		{
			System.UInt16 id = (System.UInt16) inStream.ReadObject(BigEndianUnsignedShort.TYPENAME);
			
			byte[] bytes = new byte[id];
			inStream.Stream.Read(bytes,0,(int) id);
			
			return bytes;
		}

		public virtual void Write(TypeOutputStream outStream, System.Object o, TypeElement element)
		{
			if (!(o is byte[]))
				throw new TypeException("StringType: can only write objects of type String");
			
			
			byte[] bytes = (byte[]) o;
			
			outStream.WriteObject("U16", (System.Object) bytes.Length);
			outStream.Stream.Write(bytes,0,bytes.Length);
		}
	}
}